package api

import (
	"fsd/fight"
	"encoding/json"
	"net/http"
)

type NewGameReq struct {
	GameId  uint64  `json:"game_id"`
	UserIds []int32 `json:"user_ids"`
}

type NewGameAck struct {
	Ret int    `json:"ret"`
	Msg string `json:"msg"`
}

func sendRetOk(w http.ResponseWriter) {
	r := &NewGameAck{
		Ret: RET_JSON_OK,
		Msg: "success",
	}
	retJson(w, r)
}

func sendRetFail(w http.ResponseWriter, msg string) {
	r := &NewGameAck{
		Ret: RET_JSON_FAIL,
		Msg: msg,
	}
	retJson(w, r)
}

func NewGame(w http.ResponseWriter, req *http.Request) {
	jsonReq := new(NewGameReq)
	err := json.NewDecoder(req.Body).Decode(jsonReq)
	if err != nil {
		sendRetFail(w, err.Error())
		return
	}
	if jsonReq.GameId == 0 {
		sendRetFail(w, "wrong gameid")
		return
	}
	if len(jsonReq.UserIds) == 0 {
		sendRetFail(w, "wrong userid")
		return
	}
	fight.NewGame(jsonReq.GameId, jsonReq.UserIds)
	sendRetOk(w)
}
